using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;

namespace AvaloniaCurves;

public class TickControl : Control
{
    static TickControl()
    {
        AffectsRender<TickControl>(AngleProperty);
    }

    public TickControl()
    {
        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1 / 60.0);
        timer.Tick += (sender, e) => Angle += Math.PI / 360;
        timer.Start();
    }

    public static readonly StyledProperty<double> AngleProperty =
        AvaloniaProperty.Register<TickControl, double>(nameof(Angle));        

    public double Angle
    {
        get => GetValue(AngleProperty);
        set => SetValue(AngleProperty, value);
    }

    public override void Render(DrawingContext drawingContext)
    {
        var lineLength = Math.Sqrt((100 * 100) + (100 * 100));

        var p1 = new Point(200, 200);
        var p2 = new Point(p1.X + Math.Cos(Angle)*lineLength, p1.Y + Math.Sin(Angle)*lineLength);

        var pen = new Pen(Brushes.Green, 20, lineCap: PenLineCap.Square);

        drawingContext.DrawLine(pen, p1, p2);
    }
}
